//
//  XJNetWorkRequestManager.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 ZhangXJ. All rights reserved.
//

#import "XJNetWorkRequestManager.h"


@interface XJNetWorkRequestManager ()
 ////存储回调Block的属性
 //@property (copy, nonatomic) ResultBlock block;
@end

@implementation XJNetWorkRequestManager

//请求方法 -- 使网络请求变得简便
//此处url为http://slide.sports.sina.com.cn/k/slide_2_786_99415.html?sina-fr=bd.xsy.dj&dpc=1#p=1 ？之前的部分

+ (void)RequestWithURL:(NSURL *)url andMethod:(URLRequestMethod)method andParams:(NSDictionary *)param andResult:(ResultBlock)resultBlock {
    
    //空的可变字符串 用于拼接url的参数
    NSMutableString *urlString = [NSMutableString string];
    
    //若用户传值为NSSTring类型也不报错 需要判断
    if ([url isKindOfClass:[NSURL class]]) {
        //拼接url
        [urlString appendString:url.absoluteString];
    }else if ([url isKindOfClass:[NSString class]]) {
     
        [urlString appendString:(NSString *)url];
        
    }
    if (urlString.length == 0) {
        //domain 域名      code:错误编码        userInfo字典类型
        NSError *error = [NSError errorWithDomain:@"EPA" code:251 userInfo:@{@"reason":@"网址错误"}];
        resultBlock(nil,nil,error);
        return;
    }
    //把参数字典变为字符串
    XJNetWorkRequestManager *manager = [[XJNetWorkRequestManager alloc] init];
//    manager.block = resultBlock;
    NSString *paramStr = [manager stringFromDictionary:param];
    
    switch (method) {
        case Request_GET:
        {
            if (paramStr.length > 0) {
                [urlString appendFormat:@"?%@",paramStr];
            }
            
            [urlString appendString:paramStr];
            [manager requestURL:[NSURL URLWithString:urlString] andResultBlock:resultBlock];
        }
            break;
        case Request_POST:
            [manager postURL:urlString andParamStr:paramStr andResultBlock:resultBlock];
            break;
        default:
            break;
    }
}


#pragma mark - 网络请求方式

//get请求
- (void)requestURL:(NSURL *)url andResultBlock:(ResultBlock)block {
    
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:block];
    
    [dataTask resume];
}

//post请求
- (void)postURL:(NSString *)urlStr andParamStr:(NSString *)paramStr andResultBlock:(ResultBlock)block {
   
    NSURL *url = [NSURL URLWithString:urlStr];
    
    //创建可变请求
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    
    //设置post
    [mRequest setHTTPMethod:@"POST"];
    
    //请求体
    [mRequest setHTTPBody:[paramStr dataUsingEncoding:NSUTF8StringEncoding]];
    
//    __weak typeof(self) weakSelf = self;
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:mRequest completionHandler:block];
    [dataTask resume];
}




//把参数字典变为 网址中的请求体字符串
//例如 http://slide.sports.sina.com.cn/k/slide_2_786_99415.html?sina-fr=bd.xsy.dj&dpc=1#p=1

- (NSString *)stringFromDictionary:(NSDictionary *)dict {
    
    //此字符串 以？开始
    NSMutableString *paramStr = [NSMutableString string];
    //空数组
    NSMutableArray *array = [NSMutableArray array];
    
    for (NSString *key in dict) {
        NSString *str = [NSString stringWithFormat:@"%@=%@",key,dict[key]];
        [array addObject:str];
    }
    //将数组拼接为请求体 字符串
    NSString *para = [array componentsJoinedByString:@"&"];
    
    [paramStr appendString:para];
    return paramStr;
}





@end
